//
//  HYUserViewController.m
//  HaiYue
//
//  Created by skyline on 2017/12/14.
//  Copyright © 2017年 skyline. All rights reserved.
//

#import "HYMyAssetsViewController.h"
#import "HYLoginViewController.h"
#import "HYCompensateViewController.h"
#import "HYUserPhoto.h"
#import "HYUserCenterView.h"
@interface HYMyAssetsViewController ()<HYUserCenterViewDelegate>
@property (nonatomic, strong) HYUserPhoto *userPhoto;
@property (nonatomic, strong) HYUserCenterView *userCenterView;
@property (nonatomic, strong) HYUserModel *userModel;
@end

@implementation HYMyAssetsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = THEME_COLOR;
    
    CCGradientLayer *layer = [CCGradientLayer layerWithColors:@[(__bridge id)THEME_COLOR.CGColor, (__bridge id)[UIColor whiteColor].CGColor] locations:@[@0.3, @0.5, @1.0] startPoint:CGPointMake(0, 0) endPoint:CGPointMake(0, 1.0) frame:self.view.frame];
    [self.view.layer addSublayer:layer];
    
    // Do any additional setup after loading the view.
}

- (HYUserPhoto *)userPhoto
{
    if (!_userPhoto)
    {
        _userPhoto = [[HYUserPhoto alloc]init];
        [_userPhoto addTarget:self action:@selector(actionForUserPhoto) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_userPhoto];
        [_userPhoto mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).with.offset(STATUS_HEIGHT + GAP);
            make.centerX.equalTo(self.view);
            make.size.mas_equalTo(self.userPhoto.bounds.size);
        }];
    }
    return _userPhoto;
}

- (HYUserCenterView *)userCenterView
{
    if (!_userCenterView)
    {
        _userCenterView = [[HYUserCenterView alloc]init];
        _userCenterView.delegate = self;
        [self.view addSubview:_userCenterView];
        [_userCenterView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.top.equalTo(self.userPhoto.mas_bottom).with.offset(GAP);
            make.bottom.equalTo (self.view);
            make.width.mas_equalTo(SCREEN_WIDTH - GAP * 2);
        }];
    }
    return _userCenterView;
}

- (void)setUserModel:(HYUserModel *)userModel
{
    if (_userModel != userModel)
    {
        _userModel = userModel;
        self.userPhoto.model = _userModel;
        self.userCenterView.model = _userModel;
    }
}

- (void)actionForUserPhoto
{
    NSLog(@"点击个人中心头像");
    Class vc = ([self.userModel.account length] > 1)?NSClassFromString(@"HYUserViewController"):NSClassFromString(@"HYLoginViewController");
    [self.navigationController pushViewController:[vc new] animated:YES];
}
- (void)httpRequest
{
    [CCHttp post:API_USER_MYDATA para:nil success:^(id  _Nonnull response) {
        HYUserModel *model = [HYUserModel mj_objectWithKeyValues:response];
        model.accesstoken = [HYUserManager manager].user.accesstoken;
        model.passmm = [HYUserManager manager].user.passmm;
        self.userModel = model;
        [CCSave archiver:model toFile:USER_MODEL];
    } fail:^(NSError * _Nonnull error) {
        self.userModel = [CCSave unarchiverObjFromFile:USER_MODEL];
    }];
    
    //    [HYHttps post:API_USER_MYDATA para:nil complete:^(id response) {
    //        HYUserModel *model = [HYUserModel mj_objectWithKeyValues:response];
    //        model.accesstoken = [HYUserManager manager].user.accesstoken;
    //        model.passmm = [HYUserManager manager].user.passmm;
    //        self.userModel = model;
    //        [CCSave archiver:model toFile:USER_MODEL];
    //    } fail:^(NSDictionary *dict) {
    //        self.userModel = [CCSave unarchiverObjFromFile:USER_MODEL];
    //    }];
}
#pragma mark - HYUserCenterViewDelegate
- (void)actionWithEvent:(HYUserCenterViewEvent)event
{
    Class vc = nil;
    if (event == HYUserCenterViewEventWallte)//钱包
    {
        vc = NSClassFromString(@"HYRechargeViewController");
    }
    else if (event == HYUserCenterViewEventIntegral)//积分
    {
        
    }
    else if (event == HYUserCenterViewEventRedRule)//红包规则
    {
        //        HYLoginViewController *loginVC = [[HYLoginViewController alloc] init];
        //        loginVC.type = ChangePassWordType;
        //        [self.navigationController pushViewController:loginVC animated:NO];
    }
    else if (event == HYUserCenterViewEventMaster)//拜师
    {
        //        HYLoginViewController *loginVC = [[HYLoginViewController alloc] init];
        //        loginVC.type = PayPassWordType;
        //        [self.navigationController pushViewController:loginVC animated:NO];
    }
    else if (event == HYUserCenterViewEventPrentice)//收徒
    {
        
    }
    else if (event == HYUserCenterViewEventStore)//商城
    {
        vc = NSClassFromString(@"HYStoreViewController");
    }
    else if (event == HYUserCenterViewEventPerson)//个人资料
    {
        vc = ([self.userModel.account length] > 1)?NSClassFromString(@"HYUserViewController"):NSClassFromString(@"HYLoginViewController");
    }
    else if (event == HYUserCenterViewEventNews)//消息中心
    {
        vc = ([self.userModel.account length] > 1)?NSClassFromString(@"HYNewsViewController"):NSClassFromString(@"HYLoginViewController");
    }
    else if (event == HYUserCenterViewEventSetting)//设置中心
    {
        vc = NSClassFromString(@"HYSettingViewController");
    }
    [self.navigationController pushViewController:[vc new] animated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    self.userModel = [CCSave unarchiverObjFromFile:USER_MODEL];
    [self httpRequest];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
